package com.fzj.example.socket.tcp.heartbeat;


import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * ClassName: TcpServer
 * date: 2019/11/15 15:26
 * Description: TODO
 *
 * @author fanzj@bonree.com
 * @since JDK 1.8
 */
public class TcpServer {
    public static void main(String[] args) {
        try {
            ExecutorService executorService= Executors.newFixedThreadPool(10);
            ServerSocket serverSocket=new ServerSocket(8080);
            Socket socket=null;
            while((socket=serverSocket.accept())!=null){
                Socket finalSocket = socket;
                finalSocket.setOOBInline(true);
                Runnable runnable=()->{
                    try {
                        int index=1;
                        while(true){
                          /*  if(index==6){
                                finalSocket.close();
                                System.out.println("服务器已经关闭当前通道");
                                break;
                            }*/
                            index++;
                            Thread.sleep(1000);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                };
                executorService.execute(runnable);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
